<html id="whole">
<head>
<title>简书上传工具</title>
<link rel="shortcut icon" type="image/png" href="d3.png" />	
</head>
<style>
#raw, #result {
	background-color: rgb(199,237,204);
}

#whole {
	background-color: black;
}
</style>

<textarea id="raw" rows="70" cols="80">
</textarea>
<textarea id="result" rows="70" cols="80">
Paste the raw string copied from Cloud note in the Raw area
</textarea>
<button onclick="myFunction()">Click me</button>
<script>

/*
![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)

2017-11-11 1：11PM [图片] and ![imageTag] must be matched.
*/


function getNodeTobeReplaced(current, parent, newPicUrl ){
	return {
		"current": current,
		"parent": parent,
		"newPicUrl": newPicUrl
	};
}

/*
wefsdfs
![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)
*/

var IMAGE_PATTERN = /^!\[(.*)\].*$/;
var IMAGE_NAME = /^clipboard(\d+)\..*$/;
var IMAGE_TAG = "[图片]";
var IMAGE_URL = /^.*(\(.*\))$/;

function SortedImage(index, url){
	this.index = index;
	this.url = url;
}

function sortByIndex(v1,v2){
		if(v1.index < v2.index ){
			return -1;
		}
		else if(v1.index > v2.index ){
			return 1;
		}
		else return 0;
}

function extractImage(source,raw,imagePool){
	var splitted = source.split("\n");
	for( var i = 0; i < splitted.length; i++){
		var trimed = splitted[i]; //Jerry 2019-1-19 11:25AM don't trim, or code 
		// format will be destroyed .trim();
		if( trimed.indexOf("clipboard") > 0 ) { 
			var result = IMAGE_PATTERN.exec(trimed);
			// result 0 : whole image markdown
			// result 1 : "clipboard1"
			if( result.length != 2){
				continue;
			}
			var imageName = IMAGE_NAME.exec(result[1]);
			if( imageName.length != 2) {
				continue;
			}
			var index = parseInt(imageName[1]);
			var urls = IMAGE_URL.exec(result[0]);
			if( urls.length != 2){
				alert("something bad happends :(");
			}
			var url = "![]" + urls[1];
			var storedImage = new SortedImage(index, url);
			imagePool.push(storedImage);
		}
		else{
			raw.push(trimed);
		}
	}
	imagePool.sort(sortByIndex);
}

function replaceImageTag(raw,imagePool){
	var formatted = "";
	var line = "";
	for( var i = 0; i < raw.length; i++){
		if( raw[i].indexOf(IMAGE_TAG) < 0){
			line = raw[i];
		}
		else{
			line = imagePool.shift().url;
		}
		formatted = formatted + "\n" + line;
	}
	return formatted;
}

function myFunction(){
	var source = document.getElementById("raw");
	var html = source.value;
	var rawItem = [];
	var imagePool = [];
	extractImage(html,rawItem, imagePool);
	var formatted = replaceImageTag(rawItem,imagePool);
	var target = document.getElementById("result");
	target.value = addSignature(formatted);
	copyToClipboard(target);
}

function addSignature(raw){
	var result = raw + "\n" + "\n" + "要获取更多Jerry的原创文章，请关注公众号\"汪子熙\":";
	var jerry = "![](https://upload-images.jianshu.io/upload_images/2085791-c4f58b9813a954cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
	result = result + "\n" + jerry;
	return result;
}
function copyToClipboard(node) {
  node.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    alert('Copying text command was ' + msg);
  } catch (err) {
    alert('Oops, unable to copy: ' + msg);
  }
}


</script>
</html>